import {storeToRefs} from "pinia";
import  {usePixiStore}  from '@/store/pixi'
import { toRaw } from 'vue'

import {container} from "@/pixi/container";
import * as PIXI from "pixi.js";
// pixi的app app
import {PixiApp} from '@/pixi/App'

import $bus from '@/mitt/index'

// import {Pixi} from '@/pixi/index'
import $ from "jquery";

export class ScreenResize{
    constructor() {

        this.handleResize = this.resizeFn.bind(this)

        this.resize()
    }

    resize(){
        window.addEventListener("resize",this.handleResize)
    }

    async resizeFn(){
        // $bus.emit('resizePixi')
        return
        // 销毁 pixiApp
        let store = usePixiStore()
        store.changePixiApp(null)

        let {pixiMaxWidth,pixiMaxHeight ,currentWidth,currentHeight } = storeToRefs(store)
        store.changeCurrentWidth()
        store.changeCurrentHeight()

        PixiApp.init()

        // container.x = currentWidth/ 2;
        // container.y = currentHeight/ 2;
        // container.pivot.set(0.5, 0.5);

        // 背景
        let bg = container.getChildByName('bg')
        bg.width = currentWidth.value;
        bg.height = currentHeight.value;


        // 某个精灵 位置自适应
        // let clickSprite = container.getChildByName('clickImg')
        // clickSprite.width = 100/pixiMaxWidth*currentWidth;
        // clickSprite.height = 100/pixiMaxHeight*currentHeight;

        // clickSprite.x = 200/pixiMaxWidth*currentWidth - currentWidth / 2;;
        // clickSprite.y = 200/pixiMaxHeight*currentHeight - currentHeight / 2;


        // let hello = container.getChildByName('hello')
        // hello.x = 100-currentWidth/ 2;
        // hello.y = 100-currentHeight / 2;

        // container.children.forEach(item=>{
        //     item.width = currentWidth;
        //     item.height = currentHeight;
        // })
    }

    static init(){
        let store = usePixiStore()
        let { _screenResize} = store
        if(_screenResize){
            return toRaw(_screenResize)
        }else{
            store.changeScreenResize(new ScreenResize())
            return toRaw(_screenResize)
        }
    }

    destroy(){
        window.removeEventListener('resize',this.handleResize)
    }
}
